package com.zp.business.controller.prescription;

import com.zp.business.api.prescription.bo.PrescriptionApplyBo;
import com.zp.business.api.prescription.vo.PrescriptionApplyVo;
import com.zp.business.service.prescription.IPrescriptionApplyService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 处方主表控制器
 * 前端访问路由地址为:/business/prescriptionApply
 *
 * @author zhang peng
 * @ date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/prescriptionApply")
public class PrescriptionApplyController extends BaseController {

    private final IPrescriptionApplyService iPrescriptionApplyService;

    /**
     * 查询处方主表列表 (分页)
     */
//    @SaCheckPermission("business:prescriptionApply:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PrescriptionApplyVo> selectPage (PrescriptionApplyBo bo, PageQuery pageQuery) {
        return iPrescriptionApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询处方主表列表 (不分页)
     */
//    @SaCheckPermission("business:prescriptionApply:list")
    @GetMapping("/list")
    public R<List<PrescriptionApplyVo>> list (PrescriptionApplyBo bo) {
        List<PrescriptionApplyVo> list = iPrescriptionApplyService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出处方主表列表
     */
//    @SaCheckPermission("business:prescriptionApply:export")
    @Log(title = "处方主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (PrescriptionApplyBo bo, HttpServletResponse response) {
        List<PrescriptionApplyVo> list = iPrescriptionApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "处方主表", PrescriptionApplyVo.class, response);
    }

    /**
     * 获取处方主表详细信息
     *
     * @param applyId 主键
     */
//    @SaCheckPermission("business:prescriptionApply:list")
    @GetMapping("/{applyId}")
    public R<PrescriptionApplyVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String applyId) {
        return R.ok(iPrescriptionApplyService.queryById(applyId));
    }

    /**
     * 新增处方主表
     */
//    @SaCheckPermission("business:prescriptionApply:add")
    @Log(title = "处方主表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody PrescriptionApplyBo bo) {
        return toAjax(iPrescriptionApplyService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改处方主表
     */
//    @SaCheckPermission("business:prescriptionApply:edit")
    @Log(title = "处方主表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody PrescriptionApplyBo bo) {
        return toAjax(iPrescriptionApplyService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除处方主表
     *
     * @param applyId 主键串
     */
//    @SaCheckPermission("business:prescriptionApply:remove")
    @Log(title = "处方主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applyId}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String applyId) {
        return toAjax(iPrescriptionApplyService.deleteWithValidById(applyId, true) ? 1 : 0);
    }

    /**
     * 修改收费状态
     */
//    @SaCheckPermission("business:examApply:remove")
    @Log(title = "处方主记录", businessType = BusinessType.UPDATE)
    @GetMapping("/refundApplication/{id}/{chargeFlag}")
    public R<Boolean> refundApplication (@NotEmpty(message = "主键不能为空") @PathVariable String id,
                                         @NotEmpty(message = "收费类型不能为空") @PathVariable String chargeFlag) {
        return R.ok(iPrescriptionApplyService.refundApplication(id, chargeFlag));
    }
}
